import Vue from 'vue';
import Router from 'vue-router';
import MainRouter from './Main';
import OnlineRouter from './Online';
import SpreadRouter from './Spread';
import UserRouter from './User';
import API from '../request/api';
import request from '../request/request';
import store from '../store/store';
import loginStore from '@/store/login.js';
import { Toast } from 'vant';

Vue.use(Router);
let routers = [];
routers = routers.concat(MainRouter, OnlineRouter, SpreadRouter, UserRouter);
const router = new Router({
    mode: 'history',
    routes: routers
});
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
router.beforeEach((to, from, next) => {
    //处理聚合系统回退至普通系统，重新使用普通系统的system访问welcome
    // if (!to.meta.isPoly && from.meta.isPoly && to.name !== 'welcome') {
    //     let lastSystem = JSON.parse(localStorage.getItem('lastSystem'));
    //     next({
    //         name: 'welcome',
    //         query: {
    //             system: lastSystem
    //         }
    //     });
    // }

    if (to.name === 'welcome4flutter') {
        next();
    }

    if (to.name == 'withDraw') {
        // console.log('router------', 'to, from,', to, from, 'window', window.location.search)
        let token = getQueryString("token");
        // let openid = getQueryString("openid");
        // if (openid) {
        //     let headimgurl = getQueryString("headimgurl");
        //     let nickname = decodeURI(getQueryString("nickname"));
        //     let obj = {
        //         openid: openid,
        //         headimgurl: headimgurl,
        //         nickname: nickname
        //     }
        //     store.commit('setBindWxInfo', obj)
        //     console.log('router----openid--state', loginStore)
        // }
        if (token) {

            let obj = {
                token: token,
            }
            store.commit('setBindWxInfo', obj)
            console.log('router----openid--state', loginStore)
        } else {
            store.commit('setBindWxInfo', {})
        }
        // console.log('router----openid--', loginStore)

    }
    let ROUTER_GUARD = ['news', 'spread', 'user', 'order-list-new']; // 未登录可以访问的页面
    if (ROUTER_GUARD.indexOf(to.name) >= 0 && loginStore.state.system != '9999') {
        //进这些页面先更新下本地userinfo
        request.get(API.getUserInfo).then((res) => {
            store.commit('setUserInfo', res);
            // 查一下有没有购买没有购买就不让进
            if (res.isbuy === '1') {
                next();
            } else {
                Toast('请购买商品后查看');
            }
        });
        // next();
    } else {
        next();
        //     // request.get(API.islogin).then(res => {
        //     //     // 看是否登录状态
        //     //     if (res.islogin === 1) {
        //     //         //登录状态
        //     //         next();
        //     //     } else if (res.islogin === 0) {
        //     //         //未登录状态
        //     //         // 弹出登录窗口
        //     //         loginStore.mutations.loginShow(loginStore.state, true);
        //     //     }
        //     // });
    }
});
export default router;
